package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import implementor.CategoryImp;
import implementor.ModelImp;
import implementor.PurchaseImp;
import implementor.SellerInfoImp;
import daoInterface.CategoryDao;
import daoInterface.ModelDao;
import daoInterface.PurchaseDao;
import daoInterface.SellerInfoDao;
import dto.Category;
import dto.Purchase;
import dto.SellerInfo;
import model.SalesReport;
import model.CustomerReportItems;
import model.SalesReportItems;
import utilities.AppUtilites;
import utilities.ViewUtilites;
import views.report.SalesReportView;

public class SalesReportController implements ActionListener{
	private SalesReportView view;
	private SalesReport model;
	
	public SalesReportController() {
	
	}
	
	public SalesReportController(SalesReportView view, SalesReport model) {
		super();
		this.view = view;
		this.model = model;
		AppUtilites.setSellerNameCombo(view.getSellernamecombo());
		AppUtilites.setCategoryCombo(view.getCategorycombo());
		view.actionlisteners(this);
	}

	public SalesReportView getView() {
		return view;
	}

	public void setView(SalesReportView view) {
		this.view = view;
		view.actionlisteners(this);
	}

	public SalesReport getModel() {
		return model;
	}

	public void setModel(SalesReport model) {
		this.model = model;
	}
	
	
	public void reactOnFilter()
	{
		Date fromdate=ViewUtilites.convert(view.getTfromdate().getText());
		Date todate=ViewUtilites.convert(view.getTtodate().getText());
		String catname=view.getCategorycombo().getSelectedItem().toString();
		String sellername=view.getSellernamecombo().getSelectedItem().toString();
		
		ModelDao models=new ModelImp();
		SalesReportItems[] data= models.retriveAccountReports(fromdate, todate, catname, sellername);
		String[] cols={"mCode","catName","sellerName","purchaseDate","purchasePrice","purchaseQuant","purchaseAmt","sellAmt","remainingQuant"};
		DefaultTableModel dm = (DefaultTableModel) view.getTable().getModel();
		for(int i=0;i<data.length;i++)
		{
			Object[]obj={data[i].getmCode(),data[i].getCatName(),data[i].getSellerName(),data[i].getPurchaseDate(),
							data[i].getPurchasePrice(),data[i].getPurchaseQuant(),data[i].getPurchaseQuant(),data[i].getSellAmt(),data[i].getRemainingQuant()};
			dm.addRow(obj);
		}
	
			
		
		
		
		ViewUtilites.setTable(data, cols,view.getTable());	
	}
	public void reactOnPrint()
	{
		
	}

	@Override
	public void actionPerformed(ActionEvent a) {
		String k=a.getActionCommand();
	
		if(k.contains("APPLY FILTER"))
		{
			reactOnFilter();
		}
		
		if(k.contains("Print"))
		{
			reactOnPrint();
		}
		if(k.equals("Cl1"))
		{
			ViewUtilites.makeDateComponent(view.getTfromdate());
		}
		if(k.equals("Cl"))
		{
			ViewUtilites.makeDateComponent(view.getTtodate());
		}	
	}
}
																				